/******
* Highlight.cpp
*
* Accepts the difference array and strings consisting of 
* all the text from the compared documents and formats the file 
* into a string that adheres to rtf files with the differences
* collected highlighted throughout
******/

#include <utility>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

template<class T> std::pair<std::string, std::string> printHighlighted(std::vector<std::pair<T,T> > c, std::string textA, std::string textB)
{
        //Variables instanciated
        int i, j = 0;
        bool highlightFlag = 0;
        std::string temp = "";
	  int negOneCounter = 0;
        std::string highlightA = "{\\rtf1\\ansi\\deff0{\\colortbl;\\red0\\green0\\blue0;\\red255\\green200\\blue0;}";
        std::string highlightB = "{\\rtf1\\ansi\\deff0{\\colortbl;\\red0\\green0\\blue0;\\red255\\green200\\blue0;}";
        //loop through the first text file and highlight the lines where disparities are found
        for (i = 0; i<textA.length(); i++)
        {
		    if(textA[i] != '\\')
                temp += textA[i];

                if (j < c.size())
                {
                        if(i == (c[j].first))
                        {
                                highlightFlag = 1;
                                j++;
                        }
                        else{
                                if(c[j].first == -1){
                                        if(i == c[j].second){
                                                highlightFlag = 1;
                                                j++;
												negOneCounter++;
                                        }
                                }
                        }
                }
		    if(textA[i] == '\n' || i == textA.length()-1 || textA[i] == ' ' )
                {
				while((negOneCounter > 0) && (textA[i] != ' '))
				{
					temp += " ";
					negOneCounter--;
				}
                        if(highlightFlag == 1)
                        {
                                highlightA+= "\\cf2 " + temp;
                        }
                        else
                        {
                                highlightA+= "\\cf1 " + temp;

                        }
						if(textA[i] == '\n')
							highlightA+= "\\line ";
                        highlightFlag = 0;
                        temp = "";
                }

        }
        //close RTF
        highlightA += "}";
        j = 0;
        highlightFlag = 0;
	  negOneCounter = 0;
        
        //loop through the second text file and highlight the lines where disparities are found
        for (i = 0; i<textB.length(); i++)
        {
                temp += textB[i];
                if( j < c.size())
                {
                        if(i == (c[j].second))
                        {
                                highlightFlag = 1;
                                j++;
                        }
                        else{
                                if(c[j].second == -1){
                                        if(i == c[j].first){
                                                highlightFlag = 1;
                                                j++;
												negOneCounter++;
                                        }
                                }
                        }
                }
		    if(textB[i] == '\n' || i == textB.length()-1 || textB[i] == ' ')
                {
				while((negOneCounter > 0)  && (textA[i] == ' '))
				{
					temp += " ";
					negOneCounter--;
				}
                        if(highlightFlag == 1)
                        {
                                highlightB+= "\\cf2 " + temp;
                        }
                        else
                        {
                                highlightB+= "\\cf1 " + temp;

                        }
						if(textB[i] == '\n')
							highlightB+= "\\line ";

                        highlightFlag = 0;
                        temp="";
                }

        }
        //close RTF
        highlightB += "}";
        
        return std::make_pair(highlightA,highlightB);
}